﻿#ifndef VIDEOTHREAD_H
#define VIDEOTHREAD_H

#include <QObject>
#include <QSemaphore>
#include <QThread>
#include <QMutex>
#include <QReadWriteLock>
#include <QDebug>
#include <QFileInfo>
#include <QTime>
#include <unistd.h>

#include "videodevice.h"
#include "mppencoder.h"

/*************************************************************************
 *
 *  录像
 *
 */

class VideoThread : public QThread
{
    Q_OBJECT

public:
    enum RecordStatus{
      RECORD_NULL,
      RECORD_STOP,
      RECORD_START,
      RECORD_PAUSE,
      RECORD_SIZEOVER,
      RECORDING
    };

    explicit VideoThread(VideoDevice* vd, QObject *parent = nullptr);
    ~VideoThread();

    //停止该线程运行
    void stop(void);
    //设置录像视频保存路经
    void setRecordPath(const QString & path);
    /*设置录像状态
     *          当前状态                        可改变状态
     * RECORD_NULL/RECORD_STOP     --->      RECORD_START
     * RECORD_START                --->      RECORD_STOP
     * RECORDING/RECORD_SIZEOVER   --->      RECORD_PAUSE/RECORD_STOP
     * RECORD_PAUSE                --->      RECORD_STOP/RECORDING
     */
    void setRecordStatus(RecordStatus status);

public slots:
    void videoFrameSlot(void);

signals:
    //录像完成信号
    void videoFinishSignal();
    /* 编码完成后数据
     * head == true : 表示数据包头
     */
    void encodeDataSignal(bool head, const QByteArray & pData);
    //录像时间
    //格式：HH:mm:ss
    void videoTimeSignal(const QString & time);

protected:
    void run();

private slots:
    void startRecord(void);
    void endRecord(void);
    void videoSave(const QByteArray & pArray);
    //录像计时
    void recordTiming();
    void checkRecordSizeSlot(void);
    void setStatus(RecordStatus s)
    {
        QWriteLocker write(&m_statusRWLock);
        m_recordStatus = s;
    }
    RecordStatus getStatus(void)
    {
        QReadLocker read(&m_statusRWLock);
        return m_recordStatus;
    }

private:
    bool isStop = false;

    QSemaphore m_semVideo;
    VideoDevice * m_videoDevice = nullptr;

    quint64 m_frameCnt          = 0;
    quint64 m_frameTimeSave     = 0;
    QString m_recordPath        = "";
    QString m_recordFile        = "";
    QReadWriteLock m_statusRWLock;
    RecordStatus m_recordStatus = RECORD_NULL;
    //编码
    MppEncoder * m_mppEncode    = nullptr;
    QTimer * m_checkSizeTimer   = nullptr;

    QSize m_videoSize;//图像大小
    int m_videoFps              = 0;//帧率
    int m_frameTime             = 0;//ms
};

/*************************************************************************
 *
 *  拍照
 *
 */
#include "videoffmpeg.h"

class PictureThread : public QThread
{
    Q_OBJECT
public:
    enum PictureStatus{
        PICTURE_NULL,
        PICTURE_START
    };

    explicit PictureThread(VideoDevice* vd, QObject *parent = nullptr);

    void stop(void);
    void setPicturePath(const QString & path);
    void setPictureStatus(PictureStatus status);

public slots:
    void pictureFrameSlot(void);

signals:
    void pictureFinishSignal(void);

protected:
    void run();

private:
    QString openPictureFile(void);

private:
    bool isStop = false;

    QSemaphore m_semPicture;
    VideoDevice * m_videoDevice     = nullptr;

    QString m_picturePath           = "";
    PictureStatus m_pictureStatus   = PICTURE_NULL;
};

#endif // VIDEOTHREAD_H
